﻿using QLKDS.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QLKDS.DAL
{
    public class EmployeeLevelManager
    {
        public EmployeeLevelInfo GetEmployeeLevelById(int Id)
        {
            using (var db = new QLKDSDataContext())
            {
                var queryEmployeeLevel = from d in db.EmployeeLevels
                                   where d.Id.Equals(Id)
                                   select new EmployeeLevelInfo { Id = d.Id, Description = d.Description, EmployeeId=d.EmployeeId,School=d.School,DateIssue=d.DateIssue,Specialized=d.Specialized,LevelId=d.LevelId};

                return queryEmployeeLevel.First();
            }

        }

        public IList<EmployeeLevelInfo> ListEmployeeLevel()
        {
            using (var db = new QLKDSDataContext())
            {
                var queryEmployeeLevel = from d in db.EmployeeLevels
                                         join c in db.Levels
                                         on d.LevelId equals c.Id
                                         join e in db.Employees
                                         on d.EmployeeId equals e.Id
                                         select new EmployeeLevelInfo { Id = d.Id, Description = d.Description, EmployeeId = d.EmployeeId, School = d.School, DateIssue = d.DateIssue, Specialized = d.Specialized, LevelId = d.LevelId,LevelName=c.Name,EmployeeName=e.Fullname };

                return queryEmployeeLevel.ToList();
            }
        }


        public void CreateEmployeeLevel(EmployeeLevel employeeLevel)
        {
            using (var db = new QLKDSDataContext())
            {
                db.EmployeeLevels.InsertOnSubmit(employeeLevel);

                db.SubmitChanges();

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="employeeLevel"></param>
        public void UpdateEmployeeLevel(EmployeeLevel employeeLevel)
        {
            using (var db = new QLKDSDataContext())
            {
                var _employeeLevel = db.EmployeeLevels.Single(d => d.Id.Equals(employeeLevel.Id));

                _employeeLevel.LevelId = employeeLevel.LevelId;
                _employeeLevel.School = employeeLevel.School;
                _employeeLevel.Specialized = employeeLevel.Specialized;
                _employeeLevel.DateIssue = employeeLevel.DateIssue;
                _employeeLevel.Description = employeeLevel.Description;
              
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        public void DeleteEmployeeLevel(int id)
        {
            using (var db = new QLKDSDataContext())
            {
                var _employeeLevel = db.EmployeeLevels.Single(d => d.Id.Equals(id));

                db.EmployeeLevels.DeleteOnSubmit(_employeeLevel);
                db.SubmitChanges();
            }
        }

    }
}
